<?php
session_start();
 require_once "admin/lib/class.db.php";
 $d = new db;
function checkCat($uri) {
    $pattern_detailnews = '#tintuc/[a-z0-9\-]+\-\d+.html#';
    $pattern_dmnews = '#danhmuctin/[a-z0-9\-]+\-\d+.html#';
    $pattern_detailsp = '#sanpham/[a-z0-9\-]+\-\d+.html#';
    $pattern_cat = '#danhmuc/[a-z0-9\-]+.html#';
    $pattern_cart = '#gio-hang+.html#';
    $pattern_checkout = '#thanh-toan+.html#';
     $pattern_thanhcong = '#thanh-cong+.html#';
    $pattern_about = '#about-us+.html#';
    $pattern_news = '#news+.html#';    
    $pattern_dn = '#dang-nhap+.html#'; 
    $pattern_dk = '#dang-ky+.html#';
    $pattern_qmk = '#quen-mat-khau+.html#';
    $pattern_contact = '#contact-us+.html#';

    $mod = "";
    if (preg_match($pattern_detailnews, $uri)) {
        $mod = "detail_news";
    }
    if (preg_match($pattern_dmnews, $uri)) {
        $mod = "dm_news";
    }
    if (preg_match($pattern_detailsp, $uri)) {
        $mod = "detail_sp";
    }
    if (preg_match($pattern_news, $uri)) {
        $mod = "news";
    } 
    if (preg_match($pattern_about, $uri)) {
        $mod = "about-us";
    }
   
    if (preg_match($pattern_dn, $uri)) {
        $mod = "dn";
    }
    if (preg_match($pattern_dk, $uri)) {
        $mod = "dk";
    }
    if (preg_match($pattern_qmk, $uri)) {
        $mod = "qmk";
    }
    if (preg_match($pattern_contact, $uri)) {
        $mod = "contact";
    }    
    if (preg_match($pattern_cat, $uri)) {
        $mod = "cat";
    }
    if (preg_match($pattern_cart, $uri)) {
        $mod = "cart";
    }
    if (preg_match($pattern_checkout, $uri)) {
        $mod = "checkout";
    }
    if (preg_match($pattern_thanhcong, $uri)) {
        $mod = "thanhcong";
    }
    return $mod;
}

$uri = str_replace("shop-thoitrang/", "", $_SERVER['REQUEST_URI']);
$com = checkCat($uri);
$uri = str_replace(".html", "", $uri);

$tmp_uri = explode("/", $uri);
//var_dump($tmp_uri);
if ($tmp_uri[1] == 'about-us') {
    $com = "about-us";
}
if ($tmp_uri[1] == 'sanpham') {
    $com = "detail_sp";
}

if ($tmp_uri[1] == 'contact-us') {
    $com = "contact";
}
if ($tmp_uri[1] == 'dang-nhap') {
    $com = "dn";
}
if ($tmp_uri[1] == 'dang-ky') {
    $com = "dk";
}
if ($tmp_uri[1] == 'quen-mat-khau') {
    $com = "qmk";
}
if ($tmp_uri[1] == 'news') {
    $com = "news";
}
if ($tmp_uri[1] == 'tintuc') {
    $com = "detail_news";
}
if ($tmp_uri[1] == 'danhmuctin') {
    $com = "dm_news";
}
if ($tmp_uri[1] == 'cat') {
    $com = "cat";
}
if ($com == 'cart') {
    $com = 'cart';
}
if ($com == 'checkout') {
    $com = 'checkout';
}
if ($com == 'thanhcong') {
    $com = 'thanhcong';
}
if ($com == 'cat' && $tmp_uri[1] == 'tag') {
    $com = 'tag';
}
//echo $com;
switch ($com) {
    case "contact":
        $title = "Liên hệ";
        $metaD = "Liên hệ";
        $metaK = "Liên hệ";
        break;
    case "detail_sp":
        $tieude_id = $tmp_uri[2];
        $arr = explode("-", $tieude_id);
        $sp_id = (int) end($arr);
        $rs = $modelSanPham->getDetailSP($sp_id);
        //$_SESSION['vua_xem'][$sp_id] = $sp_id;
        $row_sp = mysql_fetch_assoc($rs);

        $title = $row_sp["ten_sp"];
        $metaD = $row_sp["mo_ta"];
        $metaK = $row_sp["mo_ta"];
        break;       
    case "about-us":
        $title = "Giới thiệu";
        $metaD = "Giới thiệu";
        $metaK = "Giới thiệu";
        break;
    
    case "qmk":
        $title = " Quên mật khẩu";
        $metaD = "Quên mật khẩu";
        $metaK = "Quên mật khẩu";
        break;
    case "news":
        $title = "Tin tức";
        $metaD = "Tin tức";
        $metaK = "Tin tức";
        break;
    
    case "dk":
        $title = "Đăng ký";
        $metaD = "Đăng ký";
        $metaK = "Đăng ký";
        break;    
    case "dn":
        $title = "Đăng nhập";
        $metaD = "Đăng nhập";
        $metaK = "Đăng nhập";
        break;
    case "cart":
        $title = "Giỏ hàng";
        $metaD = "Giỏ hàng";
        $metaK = "Giỏ hàng";
        break;
    case "checkout":
        $title = "Thanh toán";
        $metaD = "Thanh toán";
        $metaK = "Thanh toán";
        break;
    case "cat":
        $tieude_id = $tmp_uri[2];
        $arr = explode("-", $tieude_id);        
        $loai_id = (int) end($arr);        
        //$Ten_KD=$arr[0].$arr[1];
        $data = $modelLoaisp->getLoaiByLoaiID( $loai_id);
        $row_cat =  mysql_fetch_assoc($data);
        $row_cat['loai_id'];
        //$row_data['Ten'];
        $title = $row_cat['loai'];
        $metaD = $data[1];
        $metaK = $data[2];
        break;  
    case "dm_news":
        $idLT1 = $tmp_uri[2];
        $arr = explode("-", $idLT1);        
        $idLT = (int) end($arr);          
        $data = $modelTin->DanhSachTinTheoLoai ($idLT);
        //$row_loaitin =  mysql_fetch_assoc($data);        
        $title = "Tin tức";
        $metaD = $data[1];
        $metaK = $data[2];
        break;
    case "detail_news":
        $tieude_id = $tmp_uri[2]; 
        $arr = explode("-", $tieude_id);      
        $idTin = (int) end($arr);
        $data = $modelTin->ChiTietTin ($idTin);
        $row_tin=  mysql_fetch_assoc($data);
       // echo $row_1['title'];die;
        $title = $data[0];
        $metaD = $data[1];
        $metaK = $data[2];
        break;

    default :
        $title = "PN Fashion Shop";
        $metaD = "PN Fashion Shop";
        $metaK = "PN Fashion Shop";
        break;
}
?>